# coding:utf8
from os import popen, mkdir, remove, system
from os.path import exists
from time import sleep, localtime, strftime


# -------- 基本参数 -----------
FileType = 'zip'                                  # 文件类型
datePrefix = '1121'                                 # 日期前缀
SOURCE_DIR = ['F:\\迅雷下载', 'D:\\迅雷下载']        # 源文件夹
TARGET_DIR = f'F:\\H_zip\\{strftime("%m%d")}\\'                            # 目标问价夹
KILL_process = 'Thunder.exe'
# --- _sup File ---支持文件 ---
f_time = './_sup/_runTime.ini'
f_list = './_sup/fileList.txt'
f_newList = './_sup/newList.txt'

if not exists(f_time): open(f_time, 'w', encoding='gbk').write('1')     # init Run Time File.
runTime = open(f_time, 'r', encoding='gbk').read()                      # 次数


def create_file_list():
    # 逻辑 Start...
    _fL = []  # File List
    if exists('./_sup/fileList.txt'): remove('./_sup/fileList.txt')
    for _dir in SOURCE_DIR: print(popen(f'dir /b /a:-D /s  {_dir}  >> ./_sup/fileList.txt').read())  # D:
    for filePath in open(f_list, 'r', encoding='gbk').read().split('\n'):
        if filePath.endswith(FileType): 
            print(filePath)
            _fL.append(filePath)   # 筛选拓展名。
    # print(_fL)
    return _fL


def move_file(file_list):
    # ----- 执行move操作 -----
    x = 1
    for i in file_list:
        ss = f'move "{i}" {TARGET_DIR}\\{datePrefix}-{runTime}-{x}.zip'
        print(ss)
        # system(f'echo {ss} >> D:\\SelfLog\\moveZIP.txt')
        print(popen(ss + f'&& echo * {ss} >> D:\\SelfLog\\moveZIP.txt').read())
        x += 1


def main():
    print(strftime("%Y-%m-%d %H:%M", localtime()))
    print(popen(f'taskkill /F /T /IM {KILL_process}').read())
    print('已终止 迅雷 进程')
    if exists(f_newList): remove(f_newList)  # Remove
    if not exists(TARGET_DIR): mkdir(TARGET_DIR)  # if not exist Dir , mkDir
    
    file_list = create_file_list()
    input(f"确认移动{len(file_list)}个文件吗？")
    for t in range(5, 0, -1): print(f'\r---{t}s 后开始...{sleep(1)}', end='', flush=True)
    move_file(file_list)
    open(f_time, 'w', encoding='gbk').write(f'{int(runTime)+1}')
    print('去删除迅雷中的已完成任务。。。')
    system(r'"C:\Program Files (x86)\Thunder Network\Thunder\Program\ThunderStart.exe" -StartType:StartMenu')
    return 0


if __name__ == '__main__':
    main()
